/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : NumberOrderProcessHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Nov 30, 2008       Raghu Sarraff			       Created
 */
package net.lwf.handler.math.number;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.entities.RadioData;
import lwf.handler.helper.DisplayFormatter;
import net.lwf.common.util.SortingUtil;
import net.lwf.entity.QuestionInfo;
import net.lwf.number.util.RandomNumberGenerator;
import net.lwf.string.util.StringManipulator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;


/**
 * Type the class description here
 *  
 */
public class NumberOrderProcessHandler extends ProcessHandler implements
		IProcessHandler {

	private static final int REQUIRED_NUMBERS = 8;
	
	public DisplayFormatter getDisplayFormatter() {
		
		return null;
	}

	public String getQuestionDesc() {
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		return this.generateSingle(false);
	}
	
	public QuestionInfo generateSingle(boolean isMultiple) {
		QuestionInfo qinfo = new QuestionInfo();
		
		List<Integer> lstData = RandomNumberGenerator.generateInts(2, REQUIRED_NUMBERS);
		String question = StringManipulator.generateString(lstData);

		int questiontype = RandomNumberGenerator.generate(1, 2);
		int answer[] = null;
		//int least = 0;
		//int highest = 0;
		if(questiontype == 1) {
			//Least to greatest
			answer = SortingUtil.sortInts(lstData, true);
			//least = answer[0];
			//highest = answer[answer.length-1];
		}
		else {
			//highest to smallest
			answer = SortingUtil.sortInts(lstData, false);
			//highest = answer[0];
			//least = answer[answer.length-1];			
		}
		qinfo.setPlaceHolder(this.generateQ(questiontype));
		String sAnswer = StringManipulator.generateCsStringFromIntArray(answer);
		qinfo.setAnswer(sAnswer);
		qinfo.setQuestion(question);
		if(!isMultiple) {
			//Set radio answers
			qinfo.setRadioAnswerChoices(this.getRadioAnswers(sAnswer));			
		}

		return qinfo;
	}
	
	private List<RadioData> getRadioAnswers(String answer) {
		List<RadioData> rdata = new ArrayList<RadioData>();
		
		rdata.add(new RadioData(answer, answer));
		
		String choice = StringManipulator.shuffleCsString(answer);
		rdata.add(new RadioData(choice, choice));
		
		choice = StringManipulator.shuffleCsString(answer);
		rdata.add(new RadioData(choice, choice));
		
		choice = StringManipulator.shuffleCsString(answer);
		rdata.add(new RadioData(choice, choice));
		
		Collections.shuffle(rdata);
		
		return rdata;
	}
	
	private String generateQ(int qType) {
		String question = "Re-write the numbers from ";
		int descType = RandomNumberGenerator.generate(1, 3);
		if(qType == 1) {
			//least to highest
			if(descType == 1) {
				question += "least to highest.";
			}
			else if(descType == 2) {
				question += "smallest to highest.";
			}
			else {
				question += "lowest to greatest.";
			}
		}
		else {
			//highest to least
			if(descType == 1) {
				question += "highest to least.";
			}
			else if(descType == 2) {
				question += "highest to smallest.";
			}
			else {
				question += "greatest to lowest.";
			}			
		}
		return question;
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		List<QuestionInfo> questions = new ArrayList<QuestionInfo>();
		int nQuestions = getRows() * getColumns();
		for(int i=0; i<nQuestions; i++) {
			QuestionInfo question = generateSingle(true);
			question.setQuestionNumber(i+1);
			questions.add(question);
		}
		return questions;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
		
		
	}

	@Override
	public int getRows() {
		
		return 0;
	}

	@Override
	public int getColumns() {
		
		return 0;
	}

	@Override
	public boolean isPortrait() {
		
		return false;
	}

	@Override
	public String getPdfInstructionalText() {
		
		return null;
	}

	@Override
	public String getPdfNote() {
		
		return null;
	}

}

/*public class NumberOrderProcessHandler extends ProcessHandler implements
		IProcessHandler {
	
	private static final int REQUIRED_NUMBERS = 8;
	
	private int least = 0;
	private int highest = 0;
	private String questionDesc = null;
	private FormatAttribute format;	
	*//**
	 * @return Returns the highest.
	 *//*
	public int getHighest() {
		return highest;
	}
	*//**
	 * @return Returns the least.
	 *//*
	public int getLeast() {
		return least;
	}
	public NumberOrderProcessHandler() {
		this.init();
	}
	

	*//**
	 * @see net.lwf.handler.IProcessHandler#getDisplayFormatter()
	 *//*
	public DisplayFormatter getDisplayFormatter() {
		format = new FormatAttribute();
		
		format.setQuestionNumber(getQuestionNum());
		format.setQuestionTable(this.questionDesc, getData1());
		format.setShowtextarea(true);
		format.setQuestionType(2); 

		return new DisplayFormatter(format);
	}
	

	*//**
	 * @see net.lwf.handler.IProcessHandler#getQuestionDesc()
	 *//*
	public String getQuestionDesc() {
		return questionDesc + " "  + getData1();
	}
	
	private void init() {
		List lstData = RandomNumberGenerator.generateInts(2, REQUIRED_NUMBERS);
		String question = StringManipulator.generateString(lstData);
		//1(question);
		int questiontype = RandomNumberGenerator.generate(1, 2);
		int answer[] = null;
		if(questiontype == 1) {
			//Least to greatest
			answer = SortingUtil.sortInts(lstData, true);
			this.least = answer[0];
			this.highest = answer[answer.length-1];
		}
		else {
			//highest to smallest
			answer = SortingUtil.sortInts(lstData, false);
			this.highest = answer[0];
			this.least = answer[answer.length-1];			
		}
		this.questionDesc = this.generateQ(questiontype);
		String sAnswer = StringManipulator.generateStringFromIntArray(answer);
		setAnswer(StringManipulator.replace(sAnswer, " ", ""));
	}
	
	private String generateQ(int qType) {
		String question = "Re-write the numbers from ";
		int descType = RandomNumberGenerator.generate(1, 3);
		if(qType == 1) {
			//least to highest
			if(descType == 1) {
				question += "least to highest.";
			}
			else if(descType == 2) {
				question += "smallest to highest.";
			}
			else {
				question += "lowest to greatest.";
			}
		}
		else {
			//highest to least
			if(descType == 1) {
				question += "highest to least.";
			}
			else if(descType == 2) {
				question += "highest to smallest.";
			}
			else {
				question += "greatest to lowest.";
			}			
		}
		return question;
	}
	//------------------------------------------------ Methods for PDF generation on the browser
	public void generate(Document document) {
	
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		return null;
	}

	@Override
	public int getColumns() {
		return 0;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
	}

	@Override
	public int getRows() {
		return 0;
	}

	@Override
	public boolean isPortrait() {
		return false;
	}
	@Override
	public String getPdfInstructionalText() {
		return null;
	}
	public boolean matchAnswer(String useranswer, AttemptedQuestion aq) {
		return false;
	}
	@Override
	public String getPdfNote() {
		return null;
	}
	
}*/

